<?php

class cacheobject {

    private static $_instance = null;

    private static $_instance_name = null;

    static public function init($with_cache = true) {
        if (! WITHOUT_CACHE && $with_cache && defined('CACHE_STORAGE') && constant('CACHE_STORAGE')) {
            self::$_instance_name = CACHE_STORAGE;
        } else {
            self::$_instance_name = 'base_cache_nocache'; // todo：增加无cache类，提高无cache情况下程序的整体性能
        }
        self::$_instance = null;
    }
 // End Function
    static public function instance() {
        if (is_null(self::$_instance)) {
            self::$_instance = kernel::single(self::$_instance_name);
        } // 使用实例时再构造实例
        return self::$_instance;
    }
 // End Function
    static public function get($key, &$return) {
        if (self::instance()->fetch(self::get_key($key), $data)) {
            if ($data['expirition'] > 0 && time() > $data['expirition']) {
                return false;
            }
            $return = $data['content'];
            return true;
        } else {
            return false;
        }
    }
 // End Function
    static public function set($key, $content, $expirition = 0) {
        $data['expirition'] = ($expirition > 0) ? $expirition : 0; // todo: 设置过期时间
        $data['content'] = $content;
        return self::instance()->store(self::get_key($key), $data);
    }
 // End Function
    static public function get_key($key) {
        $kvprefix = (defined('KV_PREFIX')) ? KV_PREFIX : '';
        $key_array['key'] = $key;
        $key_array['kv_prefix'] = $kvprefix;
        $key_array['prefix'] = 'cacheobject';
        $key_array['version'] = cachemgr::get_cache_check_version();
        return md5(serialize($key_array));
    } // End Function
}//end
